<template>
  <div class="app-container">

    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        :placeholder="lang.systemManage.params.index.filters.paramsPlaceholder"
        style="width: 350px;"
        class="filter-item"
        @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        {{ lang.systemManage.params.index.filters.searchBtn }}
      </el-button>
      <el-button v-waves class="filter-item" type="default" icon="el-icon-refresh" @click="tableReset()">
        {{ lang.systemManage.params.index.filters.refreshBtn }}
      </el-button>
      <el-button v-waves class="filter-item right-btns" type="primary" icon="el-icon-edit" @click="edit()">
        {{ lang.systemManage.params.index.operationBtn.create }}
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="listQuery.list"
      fit
      highlight-current-row
      style="width: 100%;">
      <el-table-column type="index" align="center" />
      <el-table-column :label="lang.systemManage.params.index.tableCol.name" align="left" min-width="150px">
        <template slot-scope="scope">
          <span class="link-type" @click="edit(scope.row)">{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="lang.systemManage.params.index.tableCol.code" align="left" min-width="150px" prop="code" />
      <el-table-column :label="lang.systemManage.params.index.tableCol.value" align="left" min-width="150px" prop="value" />
      <el-table-column :label="lang.systemManage.params.index.tableCol.remark" align="left" min-width="150px" prop="remark" />
      <!-- <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | moment('YYYY/MM/DD HH:mm:ss') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column :label="lang.systemManage.params.index.tableCol.operation.name" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button v-waves v-if="scope.row.status === 1 " type="danger" plain size="mini" @click="statusChose(scope.row)">禁用</el-button>
          <el-button v-waves v-else type="primary" plain size="mini" @click="statusChose(scope.row)">启用</el-button> -->
          <el-switch
            :value="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            :disabled="scope.row.$disabled"
            active-color="#13ce66"
            inactive-color="#f59292"
            @change="(model) => {statusChange(scope.row, model)}"
          />
          <span :style="{'font-size': '12px', color:scope.row.status ? '#13ce66': '#aaa', 'margin-left': '4px'}">({{ scope.row.status ? '启用' : '禁用' }})</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="listQuery.total>0"
      :total="listQuery.total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.rows"
      @pagination="getList" />
  </div>
</template>

<script>
import { getParamsPages, paramsUpdateDisabled } from '@/api/systemManage/params'
import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import DelBoxMixin from '@/mixins/delBox'
import { mapGetters } from 'vuex'

export default {
  name: 'ParamsManage',
  components: { Pagination },
  directives: { waves },
  mixins: [DelBoxMixin],
  data() {
    return {
      listLoading: true,
      listQuery: {}
    }
  },
  computed: {
    ...mapGetters([
      'lang'
    ])
  },
  created() {
    this.tableReset()
  },
  methods: {
    tableReset() {
      this.listQuery = {
        page: 1,
        rows: 10,
        total: 0,
        name: null,
        list: null
      }
      this.getList()
    },
    statusChange(item, model) {
      item.$disabled = true
      paramsUpdateDisabled(item.id, model).then(res => {
        if (res.code === 200) {
          this.$message.success('操作成功！')
          this.getList()
        } else this.$message.error(res.message)
      }).catch(e => {
        this.$message.success(this.$store.getters.errorMsg)
      })
    },
    getList() {
      this.listLoading = true
      const params = Object.assign({}, this.listQuery)
      delete params.total
      delete params.list
      getParamsPages(params).then(response => {
        console.log(response)
        this.listQuery.list = response.data.list
        this.listQuery.total = response.data.total
        this.listLoading = false
      }).catch(e => {
        this.listLoading = false
        this.$message.error('参数列表获取失败，请稍后再试！')
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    edit(item) {
      const type = item ? 'detail' : 'create'
      const id = item ? item.id : 0
      this.$router.push(`/system-manage/params-manage/${type}/${id}`)
    },
    statusChose(item) {
      paramsUpdateDisabled(item.id, item.status === 1 ? 0 : 1).then(res => {
        if (res.code === 200) {
          this.$message.success('操作成功！')
          this.getList()
        } else this.$message.error(res.message)
      }).catch(e => {
        this.$message.success(this.$store.getters.errorMsg)
      })
    }
  }
}
</script>
